package common.utils.database;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import android.os.Environment;

public class FileControlUtils {

	private static FileControlUtils fileControlUtils = new FileControlUtils();

	public static FileControlUtils getInstance() {
		return fileControlUtils;
	}
	
	
	public void getStringList(String strdata,Map<String, String> map){
		StringReader stringReader = null;
		LineNumberReader lineNumberReader = null;
		try {
			stringReader = new StringReader(strdata);
			lineNumberReader = new LineNumberReader(stringReader);

			String cacheStr = null;
			while (true) {
				cacheStr = lineNumberReader.readLine();
				if (null != cacheStr) {
					if (!cacheStr.startsWith("##")) {
						String[] s = cacheStr.split("#=#");
						map.put(s[0], s[1]);
					}
				} else {
					break;
				}
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (null != stringReader) {
					stringReader.close();
				}
				if (null != lineNumberReader) {
					lineNumberReader.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void getStringList(String dataStr, List<String> list1, List<String> list2, List<String> list3, List<String> list4, List<String> list5) {
		StringReader stringReader = null;
		LineNumberReader lineNumberReader = null;
		try {
			stringReader = new StringReader(dataStr);
			lineNumberReader = new LineNumberReader(stringReader);

			String cacheStr = null;
			while (true) {
				cacheStr = lineNumberReader.readLine();
				if (null != cacheStr) {
					if (!cacheStr.startsWith("##")) {
						if (cacheStr.startsWith("1")) {
							list1.add(cacheStr);
						} else if (cacheStr.startsWith("2")) {
							list2.add(cacheStr);
						} else if (cacheStr.startsWith("3")) {
							list3.add(cacheStr);
						} else if (cacheStr.startsWith("4")) {
							list4.add(cacheStr);
						}else if (cacheStr.startsWith("5")) {
							list5.add(cacheStr);
						}else {}
					}
				} else {
					break;
				}
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (null != stringReader) {
					stringReader.close();
				}
				if (null != lineNumberReader) {
					lineNumberReader.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public String getMainStringFromFile() {
		return getStringFromFile("/data/zekezang.org/datacache/mainData.data");
	}

	public String getStringFromFile(String path) {
		StringBuffer sb = new StringBuffer();
		File file = new File(Environment.getDataDirectory(), path);
		FileInputStream fileInputStream = null;
		try {
			fileInputStream = new FileInputStream(file);
			int i = 0;
			byte[] b = new byte[1024];
			while ((i = fileInputStream.read(b)) != -1) {
				sb.append(new String(b, 0, i));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != fileInputStream)
					fileInputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}
}
